package cn.iocoder.yudao.module.peizhen.controller.app.opencity;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.opencity.ProgramDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.module.peizhen.controller.app.opencity.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.opencity.OpenCityDO;
import cn.iocoder.yudao.module.peizhen.service.opencity.OpenCityService;

@Tag(name = "用户 APP - 服务开通城市")
@RestController
@RequestMapping("/peizhen/open-city")
@Validated
public class AppOpenCityController {

    @Resource
    private OpenCityService openCityService;



    @GetMapping("/get")
    @Operation(summary = "获得服务开通城市")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppOpenCityLocationVo> getOpenCity(@RequestParam("id") Integer id) {
        OpenCityDO openCity = openCityService.getOpenCity(id);
        return success(BeanUtils.toBean(openCity, AppOpenCityLocationVo.class));
    }



    @GetMapping("/getAllCity")
    @Operation(summary = "获取省市信息")
    @PermitAll
    public CommonResult<List<AppAreaNodeRespVO>> getAllCity() {
        List<OpenCityDO> openCityDOS = openCityService.getAllCity();
        List<AppAreaNodeRespVO> openCityRespVOS = BeanUtils.toBean(openCityDOS, AppAreaNodeRespVO.class);
        //获取省
        List<AppAreaNodeRespVO> pronvince=openCityRespVOS.stream().filter(e -> e.getType() ==2)
                .collect(Collectors.toList());
        //给省装市
        for (AppAreaNodeRespVO appareaNodeRespVO : pronvince) {
            List<AppAreaNodeRespVO> collect = openCityRespVOS.stream().filter(e -> e.getParentId().equals(appareaNodeRespVO.getId()) ).collect(Collectors.toList());
            appareaNodeRespVO.setChildren(collect);
        }
        return success(pronvince);
    }

    @GetMapping("/location")
    @Operation(summary = "通过经纬度获取城市信息")
    @PermitAll
    public CommonResult<AppOpenCityLocationVo> getCityLoaction(@RequestParam("longitude") String longitude,@RequestParam("latitude") String latitude) {
        OpenCityDO openCity = openCityService.fetchCityLocation(longitude,latitude);
        return success(BeanUtils.toBean(openCity, AppOpenCityLocationVo.class));
    }
    // ==================== 子表（具体服务） ====================

    @GetMapping("/program/list")
    @Operation(summary = "获得具体服务分页")
    @Parameter(name = "cityCode", description = "市编码")
    public CommonResult<List<ProgramDO>> getProgramList(@RequestParam("cityCode") Integer cityCode) {
        return success(openCityService.getProgramList(cityCode,null));
    }

    @GetMapping("/program/list-id")
    @Operation(summary = "获得具体服务分页")
    @Parameter(name = "cityCode", description = "市编码")
    public CommonResult<List<ProgramDO>> getProgramListById(@RequestParam("cityCode") Integer cityCode,@RequestParam(value = "projectId")Integer projectId) {
        return success(openCityService.getProgramList(cityCode,projectId));
    }




    @GetMapping("/program/get")
    @Operation(summary = "获得具体服务")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<ProgramDO> getProgram(@RequestParam("id") Integer id) {
        return success(openCityService.getProgram(id));
    }
}